<template>
    <div>
        <el-dialog center class="global-dialog" v-model="dialogVisabqleXq" width="800px" top="10vh" :show-close="true"
            :close-on-click-modal="false" @close="close">
            <template #title>
                <div class="my-header" v-loading="detail_loading">
                    <el-tabs v-model="tabId" class="demo-tabs">
                        <el-tab-pane label="基本信息" name="1">
                            <el-form :model="diaFormXq" ref="DiaFormXq" :rules="rules" status-icon label-width="120px"
                                status>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="软件名称" prop="name">
                                            <el-input placeholder="请输入" v-model="diaFormXq.name" style="width: 100%"
                                                clearable :disabled="1" />
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="首次发表日期" prop="firstPublicDate">
                                            <el-date-picker v-model="diaFormXq.firstPublicDate" type="date"
                                                value-format="YYYY-MM-DD" placeholder="选择日期" clearable style="width: 100%"
                                                :disabled="1" />
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="登记号" prop="registerNo">
                                            <el-input placeholder="请输入" v-model="diaFormXq.registerNo" style="width: 100%"
                                                clearable :disabled="1" />
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="维护单位" prop="maintainUnit">
                                            <popup-tree-input :data="orgAllList" :props="popupTreeProps" :prop="deptForm.parentName == null
                                                ? $t('message.org.top_menu')
                                                : diaFormXq.maintainUnit
                                                " :node-key="'' + deptForm.parentId"
                                                :current-change-handle="changeUnit" :disabled="1" />
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="著作权人" prop="copyOwner">
                                            <el-input placeholder="请输入" v-model="diaFormXq.copyOwner" style="width: 100%"
                                                clearable :disabled="1" />
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="发证日期" prop="issueDate">
                                            <el-date-picker v-model="diaFormXq.issueDate" type="date"
                                                value-format="YYYY-MM-DD" placeholder="选择日期" clearable style="width: 100%"
                                                :disabled="1" />
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="所属项目" prop="projectName">
                                            <el-select v-model="diaFormXq.projectName" filterable placeholder="请选择"
                                                style="width: 100%" @change="changeProgect" clearable :disabled="1">
                                                <el-option :label="item.projectname" :value="item.id" :key="item.id"
                                                    v-for="item in projectList" />
                                            </el-select>

                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="业务模块" prop="techClassId">
                                            <el-select v-model="diaFormXq.techClassId" clearable placeholder="请选择"
                                                style="width: 100%" @change="changeType" :disabled="1">
                                                <el-option :label="el.itemText" :value="el.itemValue" :key="k"
                                                    v-for="(el, k) in technologyList" />
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                                <!--  <el-row :gutter="20">
                                    <el-col :span="12">
                                        <el-form-item label="软著状态" prop="softStateId">
                                            <el-select v-model="diaFormXq.softStateId" clearable placeholder="请选择"
                                                style="width: 100%" @change="changeState">
                                                <el-option :label="el.itemText" :value="el.itemValue" :key="k"
                                                    v-for="(el, k) in awardList" />
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row> -->
                                <el-form-item label="证书存放地" prop="saveAddress">

                                    <el-input v-model="diaFormXq.saveAddress" :autosize="{ minRows: 2, maxRows: 6 }"
                                        type="textarea" placeholder="请输入" clearable :disabled="1" />
                                </el-form-item>
                                <el-form-item label="备注" prop="remark">
                                    <el-input v-model="diaFormXq.remark" :autosize="{ minRows: 2, maxRows: 6 }"
                                        type="textarea" placeholder="请输入" :disabled="1" />
                                </el-form-item>
                            </el-form>
                        </el-tab-pane>
                        <el-tab-pane label="相关附件" name="2">
                            <platformFilesxq :files="files" @filesParams="filesParams" download="KJSL:KJCG:RJZZQ:DOWNLOAD"
                                upload="KJSL:KJCG:RJZZQ:UPLOAD" fileDelete="KJSL:KJCG:RJZZQ:FILEDELET"></platformFilesxq>
                        </el-tab-pane>
                    </el-tabs>
                </div>
            </template>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="close">关闭</el-button>
                    <!-- <el-button type="primary" @click="save">保存</el-button> -->
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script >
import UserListModal from '@/views/project/components/UserListModal/index.vue';
import {
    addForm,
    editForm,
    getDetailById,
    getDictionary,
    getProject,
    getUnit, getUser
} from "@/views/science/api/achievements";
import PopupTreeInput from '@/views/science/components/PopupTreeInput/index.vue';
import PlatformFilesxq from '@/views/science/components/platformFiles/platformFilesxq.vue';
import Record from '@/views/science/components/record/record.vue';
import { tips } from "@/views/science/components/tips/tips";
import { onMounted, reactive, ref, toRefs, watch } from "vue";

// import Work from './work';

export default {
    components: {
        PlatformFilesxq, Record, UserListModal, PopupTreeInput
    },
    props: {

        orgAllList: {
            type: Array,
            required: true,
        },
        dialogVisabqleXq: {
            type: Boolean,
            default: false,
        },

        // falg  1:新增  2:编辑  3:详情
        flag: {
            type: String,
            default: "1",
        },
        id: {
            type: String,
            default: '',
        }
    },
    setup(props, { emit }) {
        watch(
            () => props.id,
            () => {
                // 深度监听.只有在id存在的时候调用详情的接口
                if (props.id) {
                    getDetail(props.id)
                }
            },
            { deep: true, immediate: true }
        );
        // 发送请求
        onMounted(() => {
            // 获取数据字典
            getUnitList();

            getWorkList()

            getProjectList()

            getCodeList()

        });

        const deptForm = {
            parentId: 0,
            parentName: '',
        }

        let DiaFormXq = ref();
        let data = reactive({
            popupTreeProps: {
                label: 'name',
                children: 'children',
            },
            tabId: '1',
            detail_loading: false,
            unitNameList: [],//维护单位
            workerList: [],//桌著作人
            projectList: [],//项目
            technologyList: [],//技术分类
            awardList: [],//软著状态
            diaFormXq: {
                id: '',
                name: '',
                firstPublicDate: "",
                registerNo: '',
                maintainUnitId: '',
                maintainUnit: '',
                copyOwnerId: '',
                copyOwner: '',
                issueDate: '',
                projectId: '',
                project: '',
                projectleader: '',
                techClassId: '',
                techClass: '',
                softState: '',
                softStateId: '',
                saveAddress: '',
                remark: '',
            },
            files: [],
            changeLogs: []
        });

        // 规则校验
        let rules = reactive({
            name: [
                { required: true, message: '请输入软件名称', trigger: 'blur' },
            ],
            firstPublicDate: [
                { required: true, message: '请选择首次发表日期', trigger: 'change', },
            ],
            maintainUnit: [
                { required: true, message: '请选择维护单位', trigger: 'change', },
            ],
            copyOwner: [
                { required: true, message: '请输入著作权人', trigger: 'change', },
            ],

            issueDate: [
                { required: true, message: '请选择发证日期', trigger: 'change', },
            ],
            projectId: [
                { required: true, message: '请选择所属项目', trigger: 'change', },
            ],
            techClassId: [
                { required: true, message: '请选择技术分类', trigger: 'change', },
            ],
            softStateId: [
                { required: true, message: '请选择软著状态', trigger: 'change' },
            ]
        });

        // 保存
        let save = () => {


            DiaFormXq.value.validate((valid) => {

                // 校验通过
                if (valid) {
                    //flag 1 新增  2 编辑  3  详情
                    if (props.flag == 1) {

                        data.diaFormXq.id = '';


                        addForm(
                            {
                                info: data.diaFormXq,
                                files: data.files,
                            }
                        ).then(() => {

                            tips("新增成功", "success");

                            DiaFormXq.value.resetFields();

                            emit("changeDialogxq", '1');

                        });


                    } else {
                        editForm({
                            info: data.diaFormXq,
                            files: data.files,
                        }).then(() => {

                            tips("编辑成功", "success");

                            DiaFormXq.value.resetFields();

                            emit("changeDialogxq", '1');

                        });

                    }
                } else {

                    return tips('请将基本信息补充完整', 'warning')

                }


            });
        }

        let succeedApproval = (item) => {
            console.log(item)
        }


        let filesParams = (params) => {

            data.files = params
        }

        let changeState = (id) => {

            let itemText = data.awardList.find(item => item.itemValue == id).itemText

            data.diaFormXq.softState = itemText

        }

        let changeType = (id) => {


            let itemText = data.technologyList.find(item => item.itemValue == id).itemText

            data.diaFormXq.techClass = itemText

        }


        let changeUnit = (item) => {

            data.diaFormXq.maintainUnitId = item.id

            data.diaFormXq.maintainUnit = item.name
        }




        let changeProgect = (id) => {

            let itemText = data.projectList.find(item => item.id == id).projectname

            data.diaFormXq.project = itemText
        }

        // 通过详情查询id

        let getDetail = async (id) => {
            data.detail_loading = true
            let res = await getDetailById(id);

            data.diaFormXq = res.data.data.info

            data.files = res.data.data.files

            data.changeLogs = res.data.data.changeLogs
            data.detail_loading = false
        };

        // 技术分类/软著状态===>数据字典

        let getCodeList = () => {

            getDictionary(['KJSL_BUSINESS_SECTOR', 'KJSL_SOFT_STATE']).then((res) => {

                data.technologyList = res.data.data.KJSL_BUSINESS_SECTOR;

                data.awardList = res.data.data.KJSL_SOFT_STATE;

            });
        };



        // 项目

        let getProjectList = () => {

            getProject({}).then((res) => {

                data.projectList = res.data.data;
            });
        };

        // 著作人

        let getWorkList = () => {

            getUser({}).then((res) => {

                data.workerList = res.data.data;
            });
        };

        // 单位
        let getUnitList = () => {

            getUnit({}).then((res) => {
                data.unitNameList = res.data.data;
            });
        };

        // 关闭清除对话框里面的数据,附件.变更记录也要删除

        let close = () => {
            data.tabId = '1';
            data.files = [];
            DiaFormXq.value.resetFields();
            emit("changeDialogxq");
        };

        return {
            DiaFormXq,
            filesParams,
            rules,
            succeedApproval,
            changeState,
            changeType,
            changeUnit,
            changeProgect,
            getUnitList,
            getWorkList,
            getProjectList,
            getCodeList,
            ...toRefs(data),
            close,
            save,
            deptForm
        };
    },
};
</script>

<style scoped lang="scss">
.dialog-footer {
    display: flex;
    justify-content: end;
}

:deep(.el-dialog__body) {
    display: none;
}

// 滚动条
:deep(.demo-tabs > .el-tabs__content) {
    max-height: 500px;
    overflow-y: scroll;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.el-col .el-select {
    width: 120px;
    margin-right: 0px !important;
}
</style>
